package com.tools;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.example.login.R;
import com.tools.goods.Goods;
import com.tools.seckillgoods.SecKillGoods;


public class HomeAllGoods extends RelativeLayout{
    private Context mContext;
    private TextView goodsRate, goodsName; // 不论Goods还是SecGoods，这两个展示相同
    private TextView goodsDiff; // 根据不同产品类型显示不同属性

    public HomeAllGoods(Context context) {
        super(context);
        mContext = context;
        initView(context);
    }
    /**
     * 带有两个参数的构造方法，布局文件使用的时候调用
     */
    public HomeAllGoods(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        mContext = context;
    }

    public HomeAllGoods(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        mContext = context;
    }

    private void initView(Context context){
        //将打气筒根据自定义控件的布局文件，创建的view对象 挂载到当前类上面 然后显示
        View view = View.inflate(context, R.layout.home_all_goods_item,this);
        goodsRate = view.findViewById(R.id.home_all_goods_rate);
        goodsName = view.findViewById(R.id.home_all_goods_name);
        goodsDiff = view.findViewById(R.id.home_all_goods_different);
    }

    /**
     * get set
     */
    public String getRate(){
        return goodsRate.getText().toString();
    }

    public void setRate(double num){
        goodsRate.setText(CommonThings.invertPercent(num));
    }

    public String getName(){
        return goodsName.getText().toString();
    }

    public void setName(String name){
        goodsName.setText(name);
    }

    public String getDiff(){
        return goodsDiff.getText().toString();
    }

    public void setDiff(String text){
        goodsDiff.setText(text);
    }

    public void setGoodsData(Goods goods){
        goodsRate.setText(CommonThings.invertPercent(goods.getAnnualInterestRate()));
        goodsName.setText(goods.getName());
        if (goods.getRiskGrade() == 0){
            goodsDiff.setText(CommonThings.getRiskGrade(goods.getRiskGrade()));
        }else if (goods.getRiskGrade() !=0 && goods.getInterestSettlementWay() == 0){
            goodsDiff.setText("到期付息");
        }
    }

    public void setSecGoodsData(SecKillGoods secKillGoods){
        goodsRate.setText(CommonThings.invertPercent(secKillGoods.getAnnualInterestRate()));
        goodsName.setText(secKillGoods.getName());
        goodsDiff.setText(String.format(mContext.getResources().getString(R.string.secKill_price), secKillGoods.getSecKillPrice()));
    }
}
